/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ManageKendaraan;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Database.database;

/**
 *
 * @author rizky
 */
@WebServlet(name="ServletEditJenisKendaraan", urlPatterns={"/ServletEditJenisKendaraan"})
public class ServletEditJenisKendaraan extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletEditJenisKendaraan</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletEditJenisKendaraan at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    @SuppressWarnings("static-access")
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String id_jenis_mobil = request.getParameter("id_jenis_mobil");
        String jenis_mobil = "";
        String kapasitas = "";
        String tarif_hari = "";
        String tarif_supir_hari = "";
        
        database connCars = new database();
        try {
            connCars.setQuery("SELECT * FROM jenis_mobil WHERE id_jenis_mobil = " + id_jenis_mobil);
            connCars.result.next();
            jenis_mobil = connCars.result.getString("jenis_mobil");
            kapasitas = connCars.result.getString("kapasitas");
            tarif_hari = connCars.result.getString("tarif_hari");
            tarif_supir_hari = connCars.result.getString("tarif_supir_hari");
            
        } catch (SQLException ex) {
            Logger.getLogger(ServletEditJenisKendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        PrintWriter out = response.getWriter();
        out.println("<form method=\"post\" action=\"ServletEditJenisKendaraan\">" +
                "<table width=\"327\">"+
                                  "<tr>"+
                                        "<th width=\"96\">Jenis Mobil</th>"+
                                        "<td width=\"152\">"+
                                        "<input type=\"hidden\" name=\"id_jenis_mobil\" value=\""+id_jenis_mobil+"\"> " +
                                          "<input type=\"text\" name=\"jenis_mobil\" value=\""+jenis_mobil+"\">"+
                                        "</td>"+
                                  "</tr>"+
                                  "<tr>"+
                                        "<th width=\"96\">Kapasitas Kendaraan</th>"+
                                        "<td width=\"152\">"+
                                          "<input type=\"text\" name=\"kapasitas\" value=\""+kapasitas+"\">"+
                                        "</td>"+
                                  "</tr>"+
                                  "<tr>"+
                                        "<th width=\"96\">Tarif charter(per hari)</th>"+
                                        "<td width=\"152\">"+
                                          "<input type=\"text\" name=\"tarif_hari\" value=\""+tarif_hari+"\">"+
                                        "</td>"+
                                  "</tr>"+
                                  "<tr>"+
                                        "<th width=\"96\">Tarif Sopir(per hari)</th>"+
                                        "<td width=\"152\">"+
                                          "<input type=\"text\" name=\"tarif_supir_hari\" value=\""+tarif_supir_hari+"\">"+
                                        "</td>"+
                                        "<td><input type=\"submit\" name=\"button7\" id=\"button7\" value=\"Simpan\" /></td>"+
                                  "</tr>"+
                                "</table>"+
                                "</form>" +
                        "       <form method=\"post\" action=\"ServletHapusJenisKendaraan\">" +
                        "           <input type=\"hidden\" name=\"id_jenis_mobil\" value=\""+id_jenis_mobil+"\"> " +
                        "           <input type=\"submit\" value=\"Hapus Jenis Kendaraan\">" +
                        "       </form>");
                //out.close();
                processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String id_jenis_mobil = request.getParameter("id_jenis_mobil");
        String jenis_mobil = request.getParameter("jenis_mobil");
        String kapasitas = request.getParameter("kapasitas");
        String tarif_hari = request.getParameter("tarif_hari");
        String tarif_supir_hari = request.getParameter("tarif_supir_hari");

        Kendaraan cars = new Kendaraan();
        int res = cars.editJenisKendaraan(id_jenis_mobil,jenis_mobil,kapasitas,tarif_hari,tarif_supir_hari);

        if(res == 1){
            response.sendRedirect("ManageCars.jsp");
        }else{
            PrintWriter out = response.getWriter();
            out.println("Gagal edit Jenis Kendaraan, ID :" + jenis_mobil +
                    kapasitas + tarif_hari + tarif_supir_hari);
            out.close();
        }
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
